package cn.sfa.common.taglib;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sfa.entity.system.FunctionSysCode;
import cn.sfa.entity.system.ModelSysCode;
import cn.sfa.entity.system.ProviderSysCode;

/**
 * 
 * 类名称：BranchCache 类描述： 机构缓存类 创建人：lenovo 创建时间：2012-3-6 上午11:45:01 修改人：lenovo
 * 修改时间：2012-3-6 上午11:45:01 修改备注：
 * 
 * @version
 */
public class CommonCache {

	static Logger logger = LoggerFactory.getLogger(CommonCache.class);

	// 机构缓存类静态实例
	private static CommonCache instance;

	// 供应商缓存数据
	private static List<ProviderSysCode> cacheProviderList;
	
	//权限模块
	private static List<ModelSysCode> cacheModelList;
	
	//权限功能
	private static List<FunctionSysCode> cacheFunctionlList;

	private static CommonService commonService;
	
	

	public CommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(CommonService commonService) {
		CommonCache.commonService = commonService;
	}

	/**
	 * 私有构造方法
	 */
	private CommonCache() {

	}

	/**
	 * 初始化
	 * 
	 * @return
	 */
	public static CommonCache getInstance() {

		logger.info("----------开始初始化机构缓存数据------------");
		// 若未有实例，则先构造
		if (instance == null) {
			instance = new CommonCache();// 构造实例
		}
		// 若实例中缓存值为空，则取缓存
		if (instance.cacheModelList == null) {
			instance.cacheModelList = instance.getAllModel();
		}
		if (instance.cacheFunctionlList == null) {
			instance.cacheFunctionlList = instance.getAllFunction();
		}

		// 返回实例
		return instance;
	}
	
	public static void setCacheFunctionlModelNull(){
		instance.cacheFunctionlList = null;
		instance.cacheModelList = null;
	}

	public List<FunctionSysCode> getAllFunction() {
		try {
			return commonService.getFunctionList();
		} catch (Exception e) {
			logger.error("无法正常获取功能URL的相关信息");
			e.printStackTrace();
			return null;
		}
	}

	public List<ModelSysCode> getAllModel() {
		try {
			return commonService.getModelList();
		} catch (Exception e) {
			logger.error("无法正常获取模块URL的相关信息");
			e.printStackTrace();
			return null;
		}
	}
}
